package cn.ven.tool;

import cn.ven.entity.MyUser;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/*
  方便获取用户信息的工具类
 */
public class Token {

    //获取用户uid
    public static Integer getUid(){
        MyUser user = (MyUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user==null){
            return 0;
        }
        return user.getId();
    }

    //获取用户信息
    public static Object getAccount() {
        Object principal =  SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal==null)  return null;
        if (principal instanceof UserDetails) {  //当是UserDtails类的时候，也就是有登录的时候
            return ((UserDetails) principal);
        } else {
            return String.valueOf(principal);
        }
    }

}
